package com.example.device

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.databinding.ActivityDeviceXiangBinding
import com.example.device.vm.DeviceOrderViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/device/DeviceXiangActivity")
class DeviceXiangActivity : BaseMvvmActivity<ActivityDeviceXiangBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_device_xiang
    @Autowired
    @JvmField
    var deviceId=0
    var inspector=""

    private val deviceOrderViewModel: DeviceOrderViewModel by viewModels()
    override fun initView() {
        super.initView()
        ARouter.getInstance().inject(this)
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        deviceOrderViewModel.getListDataOrderXiang(deviceId)
        deviceOrderViewModel.mSuccessLiveData.observe(this){
            it.data.forEach {
                mDataBinding.orderType.text=it.orderType
                mDataBinding.appointmentTime.text=it.appointmentTime
                mDataBinding.status.text=it.status
                if (it.status=="进行中"){
                    mDataBinding.status.setBackgroundResource(R.drawable.bg_ju)
                    mDataBinding.status.text="已分配"
                    mDataBinding.num.text="1/3"
                    mDataBinding.startInspection.visibility=View.VISIBLE
                    mDataBinding.transfer.visibility=View.VISIBLE

                }else if (it.status=="已完成"){
                    mDataBinding.num.text="3/3"
                    mDataBinding.status.setBackgroundResource(R.drawable.bg_hui)
                    mDataBinding.status.text="已过期"
                }else if (it.status=="已转派"){
                    mDataBinding.num.text="2/3"
                    mDataBinding.status.setBackgroundResource(R.drawable.bg_zi)
                    mDataBinding.status.text="已转派"
                }else{
                    mDataBinding.status.setBackgroundResource(R.drawable.bg_red)
                    mDataBinding.status.text="未分配"
                    mDataBinding.num.text="0/3"
                    mDataBinding.assignTasks.visibility=View.VISIBLE
                }
            }
        }
        deviceOrderViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        mDataBinding.assignTasks.setOnClickListener {
            mDataBinding.startInspection.visibility=View.VISIBLE
            mDataBinding.transfer.visibility=View.VISIBLE
            mDataBinding.assignTasks.visibility=View.GONE
        }
        //开始巡检
        mDataBinding.startInspection.setOnClickListener {
            deviceOrderViewModel.getListDataStartInspection(deviceId)
            deviceOrderViewModel.mSuccessLiveData.observe(this){
                ToastUtils.showLong("开始巡检成功")
            }
            deviceOrderViewModel.mErrorLiveData.observe(this){
                ToastUtils.showLong(it)
            }
        }
        //转派
        mDataBinding.transfer.setOnClickListener {
            deviceOrderViewModel.getListDataTransferInspection(deviceId,inspector)
            deviceOrderViewModel.mSuccessLiveData.observe(this){
                ToastUtils.showLong("转派成功")
            }
            deviceOrderViewModel.mErrorLiveData.observe(this){
                ToastUtils.showLong(it)
            }
        }

    }
}